package cn.iocoder.yudao.module.system.controller.admin.lottery;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.lottery.vo.LotteryRespVO;
import cn.iocoder.yudao.module.system.controller.admin.lottery.vo.LotteryRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.task.vo.UserTaskRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.task.vo.TaskStatusRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.task.UserTaskRecordDO;
import cn.iocoder.yudao.module.system.service.lottery.LotteryService;
import cn.iocoder.yudao.module.system.service.task.UserTaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 抽奖 Controller
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - 抽奖")
@RestController
@RequestMapping("/system/lottery")
@Validated
@Slf4j
public class LotteryController {

    @Resource
    private LotteryService lotteryService;

    @Resource
    private UserTaskService userTaskService;

    @PostMapping("/draw")
    @Operation(summary = "用户抽奖")
    @PermitAll
    public CommonResult<LotteryRespVO> drawLottery() {
        Long userId = getLoginUserId();
        if (userId==null)
            return CommonResult.error(404,"请先登录");
        return success(lotteryService.drawLottery(userId));
    }

    @GetMapping("/records")
    @Operation(summary = "获取用户抽奖记录")
    @PermitAll
    public CommonResult<List<LotteryRecordRespVO>> getUserLotteryRecords() {
        Long userId = getLoginUserId();
        return success(lotteryService.getUserLotteryRecords(userId));
    }

    @GetMapping("/can-draw")
    @Operation(summary = "检查用户是否还能抽奖")
    @PermitAll
    public CommonResult<Boolean> canDrawLottery() {
        Long userId = getLoginUserId();
        return success(lotteryService.canDrawLottery(userId));
    }

    @GetMapping("/remaining-count")
    @Operation(summary = "获取用户今日剩余抽奖次数")
    @PermitAll
    public CommonResult<Integer> getRemainingLotteryCount() {
        Long userId = getLoginUserId();
        return success(lotteryService.getRemainingLotteryCount(userId));
    }

    @PostMapping("/complete-share-task")
    @Operation(summary = "完成分享任务获取抽奖次数")
    @PermitAll
    public CommonResult<Boolean> completeShareTask() {
        Long userId = getLoginUserId();
        boolean success = lotteryService.completeShareTaskForLottery(userId);
        return success(success);
    }

    @PostMapping("/complete-invite-task")
    @Operation(summary = "完成邀请好友任务获取抽奖次数")
    @PermitAll
    public CommonResult<Boolean> completeInviteFriendTask() {
        Long userId = getLoginUserId();
        boolean success = lotteryService.completeInviteFriendTaskForLottery(userId);
        return success(success);
    }

    @PostMapping("/complete-profile-task")
    @Operation(summary = "完成个人信息完善任务获取抽奖次数")
    @PermitAll
    public CommonResult<Boolean> completeProfileTask() {
        Long userId = getLoginUserId();
        boolean success = lotteryService.completeProfileTaskForLottery(userId);
        return success(success);
    }

    @GetMapping("/task-records")
    @Operation(summary = "获取用户任务记录")
    @PermitAll
    public CommonResult<List<UserTaskRecordRespVO>> getUserTaskRecords() {
        Long userId = getLoginUserId();
        List<UserTaskRecordDO> records = userTaskService.getUserTaskRecords(userId);
        return success(BeanUtils.toBean(records, UserTaskRecordRespVO.class));
    }

    @GetMapping("/task-status")
    @Operation(summary = "获取用户任务状态")
    @PermitAll
    public CommonResult<List<TaskStatusRespVO>> getUserTaskStatus() {
        Long userId = getLoginUserId();
        List<TaskStatusRespVO> taskStatus = userTaskService.getUserTaskStatus(userId);
        return success(taskStatus);
    }
}
